﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using SongSharp.Domain.Objects;

namespace SongSharp.Domain.Queueing
{
    public class PlayingQueue
    {
        private const int INIT_CAPACITY_QUEUE = 300;
        private const uint MAX_TRACK_NUMBER = 30;
        private const uint MIN_TRACK_NUMBER = 10;

        private int CurrentTrackIndex { get; set; }

        // Wraps public access to the track index
        public int GetCurrentTrackIndex()
        {
            return CurrentTrackIndex;
        }

        private List<Track> Queue { get; set; }

        public PlayingQueue() 
        {
            Queue = new List<Track>(INIT_CAPACITY_QUEUE);
            CurrentTrackIndex = -1;
        }

        public void Enqueue(Track track)
        {
            Queue.Add(track);
        }

        public Track Peek()
        {
            try
            {
                return Queue.ElementAt(CurrentTrackIndex);
            }
            catch (ArgumentOutOfRangeException)
            {
                return null;
            }
        }

        public void EnqueueRange(List<Track> list)
        {
            Queue.AddRange(list);
        }

        public Track Dequeue()
        {
            Track track = null;
            if (Queue.Count > CurrentTrackIndex + 1)
            {
                CurrentTrackIndex++;
                track = Peek();
            }
            return track;
        }

        public void Insert(int index, Track track)
        {
            try
            {
                Queue.Insert(index + CurrentTrackIndex, track);
            }
            catch (ArgumentOutOfRangeException)
            {
                Queue.Insert(CurrentTrackIndex, track);
            }
        }

        public Track DequeuePrevious()
        {
            if (CurrentTrackIndex > 0)
                CurrentTrackIndex--;
            return Peek();
        }

        public void Clear()
        {
            Queue.Clear();
            CurrentTrackIndex = -1;
        }

        public List<Track> ToList()
        {
            return Queue.ToList();
        }
    }
}
